package com.lfy.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.auth.domain.entity.UserDetailEntity;
import com.lfy.auth.domain.entity.UserEntity;
import com.lfy.auth.domain.req.ChangeNameReq;
import com.lfy.auth.domain.req.ChangeSignatureReq;
import com.lfy.auth.domain.resp.FindFriendResp;
import com.lfy.auth.mapper.UserDetailMapper;
import com.lfy.auth.mapper.UserMapper;
import com.lfy.auth.service.UserDetailService;
import com.lfy.common.constant.GlobalConstant;
import com.lfy.common.enums.BaseEnum;
import com.lfy.common.result.R;
import com.lfy.commons.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
* @author 吕飞扬
* @description 针对表【user_detail】的数据库操作Service实现
* @createDate 2023-10-19 19:16:42
*/
@Service
@Slf4j
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetailEntity>
    implements UserDetailService{
    @Resource
    private MinioUtil minioUtil;
    @Resource
    private UserMapper userMapper;
    @Override
    public R<Void> changeName(UserEntity user, ChangeNameReq req) {
        Long id = user.getId();
        String name = req.getName();
        UserDetailEntity userDetailEntity = new UserDetailEntity();
        userDetailEntity.setName(name);
        if( this.baseMapper.update(userDetailEntity, new LambdaQueryWrapper<UserDetailEntity>().eq(UserDetailEntity::getUserId, id)) == GlobalConstant.I_1){
            return R.success();
        }
        return R.error(BaseEnum.commonEnum.SYSTEM_ERROR);
    }

    @Override
    public R<Void> changeSignature(UserEntity user, ChangeSignatureReq req) {
        Long id = user.getId();
        String signature = req.getSignature();
        UserDetailEntity userDetailEntity = new UserDetailEntity();
        userDetailEntity.setSignature(signature);
        if (this.baseMapper.update(userDetailEntity,new LambdaQueryWrapper<UserDetailEntity>().eq(UserDetailEntity::getUserId,id)) == GlobalConstant.I_1) {
            return R.success();
        }
        return R.error(BaseEnum.commonEnum.SYSTEM_ERROR);

    }

    @Override
    public R<FindFriendResp> findFriend(String sign) {
        UserEntity userEntity = userMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUserName, sign));
        if(Objects.isNull(userEntity)){
            return R.success();
        }
        UserDetailEntity userDetailEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<UserDetailEntity>().eq(UserDetailEntity::getUserId, userEntity.getId()));
        if(Objects.isNull(userDetailEntity)){
            log.error("User:{} data not initialized",userEntity.getId());
            return R.success();
        }
        FindFriendResp findFriendResp = new FindFriendResp();
        BeanUtils.copyProperties(userDetailEntity,findFriendResp);
        findFriendResp.setId(userDetailEntity.getUserId());
        return R.success(findFriendResp);
    }


}




